{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Singly Linked List Cycle Check \n",
    "\n",
    "## Problem\n",
    "\n",
    "Given a singly linked list, write a function which takes in the first node in a singly linked list and returns a boolean indicating if the linked list contains a \"cycle\".\n",
    "\n",
    "A cycle is when a node's next point actually points back to a previous node in the list. This is also sometimes known as a circularly linked list.\n",
    "\n",
    "You've been given the Linked List Node class code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Node(object):\n",
    "    \n",
    "    def __init__(self,value):\n",
    "        \n",
    "        self.value = value\n",
    "        self.nextnode = None"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Solution\n",
    "\n",
    "Fill out your solution:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cycle_check(node):\n",
    "#     Use fast and slow pointer\n",
    "    fast, slow = node, node\n",
    "    while fast and fast.nextnode:\n",
    "        fast = fast.nextnode\n",
    "        if fast == slow:\n",
    "            return True\n",
    "        fast = fast.nextnode\n",
    "        slow = slow.nextnode\n",
    "    return False\n",
    "    pass #Your function should return a boolean"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test Your Solution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ALL TEST CASES PASSED\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "RUN THIS CELL TO TEST YOUR SOLUTION\n",
    "\"\"\"\n",
    "from nose.tools import assert_equal\n",
    "\n",
    "# CREATE CYCLE LIST\n",
    "a = Node(1)\n",
    "b = Node(2)\n",
    "c = Node(3)\n",
    "\n",
    "a.nextnode = b\n",
    "b.nextnode = c\n",
    "c.nextnode = a # Cycle Here!\n",
    "\n",
    "\n",
    "# CREATE NON CYCLE LIST\n",
    "x = Node(1)\n",
    "y = Node(2)\n",
    "z = Node(3)\n",
    "\n",
    "x.nextnode = y\n",
    "y.nextnode = z\n",
    "\n",
    "\n",
    "#############\n",
    "class TestCycleCheck(object):\n",
    "    \n",
    "    def test(self,sol):\n",
    "        assert_equal(sol(a),True)\n",
    "        assert_equal(sol(x),False)\n",
    "        \n",
    "        print (\"ALL TEST CASES PASSED\")\n",
    "        \n",
    "# Run Tests\n",
    "\n",
    "t = TestCycleCheck()\n",
    "t.test(cycle_check)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Good Job!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
